package com.mohe.shanpao.jetty;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.NetworkTrafficServerConnector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class FirstServer {

	public static void main(String[] args) throws Exception {
		Server server = new Server();
		
		ServerConnector connector = new NetworkTrafficServerConnector(server);
		connector.setHost("127.0.0.1");
		connector.setPort(9990);
		connector.setName("http-server");
		
		server.addConnector(connector);
		
		server.setHandler(new MyHandler());
		
		server.start();
		
		server.join();
	}
	
	static class MyHandler extends AbstractHandler
	{

		@Override
		public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
				throws IOException, ServletException {
			response.setContentType("text/html;charset=utf-8");
			response.setStatus(HttpServletResponse.SC_OK);
			baseRequest.setHandled(true); 
			response.getWriter().println("<h1>Hello</h1>");
		}
		
	}
}
